package com.xneure.zkweb.view;

import com.xneure.enging.ui.layout.Layout;
import com.xneure.enging.ui.layout.Layout.Orientation;
import com.xneure.enging.widget.Control;
import com.xneure.enging.widget.event.ChangeEvent;
import com.xneure.enging.widget.event.ClickEvent;
import com.xneure.enging.widget.grid.Column;
import com.xneure.enging.widget.grid.Grid;
import com.xneure.enging.widget.grid.Row;
import com.xneure.enging.widget.grid.Title;
import com.xneure.enging.widget.impl.Button;
import com.xneure.enging.widget.impl.CheckBox;
import com.xneure.enging.widget.impl.DialogBox;
import com.xneure.enging.widget.impl.Label;
import com.xneure.enging.widget.impl.Page;
import com.xneure.enging.widget.impl.TextBox;
import com.xneure.enging.widget.menu.Menu;
import com.xneure.enging.widget.menu.MenuItem;
import com.xneure.enging.widget.tabs.Tab;
import com.xneure.enging.widget.tabs.Tabs;
import com.xneure.enging.widget.tree.Tree;
import com.xneure.enging.widget.tree.TreeItem;
import com.xneure.utils.bean.GUID;

public class TestPage extends Page{
	private CheckBox checkBox;
	private Button button;
	private Layout layout;
	private Label label;
	private int clickCount = 0;
	private Tabs tabs;

	public TestPage(Control parent){
		super(parent);
		
		checkBox = new CheckBox(this);
		label = new Label(this);
		checkBox.addEvent(new ChangeEvent(){
			@Override
			public void event(Object source){
				if(checkBox.isChecked()){
					layout.setOrientation(Orientation.horizontal);
					layout.setStyle("width","300px");
					label.setText("横向");
				}else{
					layout.setOrientation(Orientation.vertical);
					layout.setStyle("width","100px");
					label.setText("垂直");
				}
			}
		});
		label.setText("横向");
		button = new Button(this);
		button.setHeight("28px");
		button.setWidth("50px");
		button.addEvent(new ClickEvent(){
			@Override
			public void event(Object source){
				
				switch(clickCount){
					case 0:
						TextBox t = new TextBox(layout);
						t.setWidth("100px");
						t.setHeight("20px");
						t.setValue(GUID.randomID().toString());
						Label lab = new Label(layout);
						lab.setStyle("width","100px");
						lab.setStyle("height","20px");
						lab.setText(GUID.randomID().toString());
						break;
					case 1:
						Grid grid = new Grid(getPage());
						grid.setStyle("width","100%");
						addTitle(grid);
						for(int i = 0;i < 5;i++){
							getRow(grid,i);
						}
						break;
					case 2:
						getTree(getPage());
						break;
					case 3:
						getMenu(getPage());
						break;
					default:
						DialogBox dialogBox = new DialogBox(getPage());
						dialogBox.setDetails("Demo已经加载完成，请刷新后重新加载。");
						dialogBox.show(true);
						break;
				}
				clickCount++;
			}
		});
		layout = new Layout(this);
		layout.setOrientation(Orientation.vertical);
	}

	private void initTab(TestPage testPage){
		tabs = new Tabs(testPage);
		tabs.setHeight("250px");
		for(int i = 0;i < 5;i++){
			Tab t = new Tab(tabs);
			t.setTitle(i + " 标题");
		}
	}

	private Tree getTree(Page page){
		Tree t = new Tree(page);
		for(int i = 0;i < 5;i++){
			TreeItem ti = new TreeItem(t);
			ti.setTitle("标题-" + i);
			getTreeItem(ti,i);
		}
		return t;
	}

	private void getTreeItem(TreeItem item,int i2){
		for(int i = 0;i < 3;i++){
			TreeItem ti = new TreeItem(item);
			ti.setTitle("2级标题-" + i2 + " " + i);
			getTreeItem1(ti,i);
		}
	}

	private void getTreeItem1(TreeItem item,int i2){
		for(int i = 0;i < 2;i++){
			TreeItem ti = new TreeItem(item);
			ti.setTitle("3级标题-" + i2 + " " + i);
		}
	}

	private Menu getMenu(Page page){
		Menu t = new Menu(page);
		for(int i = 0;i < 5;i++){
			MenuItem ti = new MenuItem(t);
			ti.setTitle("1级菜单-" + i);
			getMenuItem(ti,i);
		}
		return t;
	}

	private void getMenuItem(MenuItem item,int i2){
		for(int i = 0;i < 3;i++){
			MenuItem ti = new MenuItem(item);
			ti.setTitle("2级菜单-" + i2 + " " + i);
			getMenuItem1(ti,i);
		}
	}

	private void getMenuItem1(MenuItem item,int i2){
		for(int i = 0;i < 2;i++){
			MenuItem ti = new MenuItem(item);
			ti.setTitle("3级菜单-" + i2 + " " + i);
		}
	}

	private Row getRow(Grid grid,int r){
		Row row = new Row(grid);
		for(int i = 0;i < 5;i++){
			Column col = new Column(row);
			col.setValue(String.format("  行 %1$s 列 %2$s  ",r + 1,i + 1));
		}
		return row;
	}

	private void addTitle(Grid grid){
		Title row = new Title(grid);
		for(int i = 0;i < 5;i++){
			Column col = new Column(row);
			col.setValue("标 " + (i + 1));
		}
	}

	private Page getPage(){
		return this;
	}
}
